package com.itlong.cloud.controller.huawei;

import com.itlong.cloud.POJO.PO.HuaweiOrderInfoPO;
import com.itlong.cloud.annotation.UnAuthAccessRequired;
import com.itlong.cloud.enumerate.HuaweiOrderTypeEnum;
import com.itlong.cloud.paymenthub.IPaymentTradeServiceClient;
import com.itlong.cloud.utils.encrypt.HuaweiEncryptUtil;
import com.itlong.cloud.utils.node.JsonUtil;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by PPP on 2020/4/7.
 */
@RestController
@RequestMapping("/huawei/valid")
public class StoreBackController {
    private static Logger LOG = LoggerFactory.getLogger(StoreBackController.class);
    String key;
    @Autowired
    IPaymentTradeServiceClient iPaymentTradeServiceClient;
    /**
     * <desc>
     *      订购商品回调
     * </desc>
     *
     * @param request
     * @return
     * @author Jiaqi.X
     * @createDate 2020/04/07
     */
    @RequestMapping("/newInstance")
    @ResponseBody
    @UnAuthAccessRequired
    public Object valid(HttpServletRequest request,HttpServletResponse response) throws IOException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException {
        Map<String,Object> result = new HashMap<>();
        result.put("resultCode","000000");
        result.put("resultMsg","success");
        result.put("instanceId", LogicIdUtil.bussinessId());
        result.put("encryptType","2");
        Map<String,Object> appInfo = new HashMap<>();
        appInfo.put("frontEndUrl","xxx");
        appInfo.put("adminUrl","xxx");
        appInfo.put("userName",HuaweiEncryptUtil.generateSaaSUsernameOrPwd("908fc823-65e0-47e2-b520-18860e618e37", "xxxxxxxxsxxxxxxxxxxxx", 128));
        appInfo.put("password",HuaweiEncryptUtil.generateSaaSUsernameOrPwd("908fc823-65e0-47e2-b520-18860e618e37", "xxxxxxxxsxxxxxxxxxxxx", 128));
        result.put("appInfo",appInfo);
        response.addHeader("sign_type", "\"HMAC-SHA256\"");
        response.addHeader("Body-Sign","sign_type=\"HMAC-SHA256\""+",signature=\""+ HuaweiEncryptUtil.base_64(HuaweiEncryptUtil.hmacSHA256("908fc823-65e0-47e2-b520-18860e618e37", JsonUtil.toJSON(result)))+"\"");
        try {
            String mobilePhone = HuaweiEncryptUtil.decryptMobilePhoneOrEMail(key, request.getParameter("mobilePhone"), 128);
            String email = HuaweiEncryptUtil.decryptMobilePhoneOrEMail(key, request.getParameter("email"), 128);
            String customerName = request.getParameter("customerName");
            LOG.info("【SaaS下单入参】:" + String.format("activity:%s;businessId:%s;customerId:%s;customerName:%s;email:%s;expireTime:%s;" +
                    "mobilePhone:%s;orderId:%s;productId:%s;testFlag:%s;timeStamp:%s;userId:%s;userName:%s;authToken:%s; amount:%s", request.getParameter("activity"), request.getParameter("businessId")
                    , request.getParameter("customerId"), customerName, email, request.getParameter("expireTime"), mobilePhone, request.getParameter("orderId")
                    , request.getParameter("productId"), request.getParameter("testFlag"), request.getParameter("timeStamp"), request.getParameter("userId"), request.getParameter("userName"), request.getParameter("authToken"), request.getParameter("amount")));
            //保存数据
            HuaweiOrderInfoPO po = new HuaweiOrderInfoPO();
            po.setActivity(request.getParameter("activity"));
            po.setBusinessId(request.getParameter("businessId"));
            po.setCustomerId(request.getParameter("customerId"));
            po.setCustomerName(customerName);
            po.setEmail(email);
            po.setExpireTime(request.getParameter("expireTime"));
            po.setMobilePhone(mobilePhone);
            po.setOrderId(request.getParameter("orderId"));
            po.setProductId(request.getParameter("productId"));
            po.setTestFlag(request.getParameter("testFlag"));
            po.setTimeStamp(request.getParameter("timeStamp"));
            po.setUserId(request.getParameter("userId"));
            po.setUserName(request.getParameter("userName"));
            po.setAuthToken(request.getParameter("authToken"));
            po.setStatus(1);
            po.setCreateTime(new Date());
            po.setOrderType(HuaweiOrderTypeEnum.WELINK.getType());
            iPaymentTradeServiceClient.saveHuaweiSaaSData(po);
        }catch ( Exception e){
            LOG.error("保存welink客户订单数据失败");
        }
        return result;
    }

//    /**
//     * <desc>
//     *
//     * </desc>
//     *
//     * @param request
//     * @return
//     * @author Jiaqi.X
//     * @createDate 2020/04/07
//     */
//    @RequestMapping("/newInstanceCount")
//    @ResponseBody
//    @UnAuthAccessRequired
//    public Object newInstanceCount(HttpServletRequest request,HttpServletResponse response) throws IOException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException {
//        Map<String,Object> result = new HashMap<>();
//        result.put("resultCode","000000");
//        result.put("resultMsg","success");
//        result.put("instanceId", LogicIdUtil.bussinessId());
//        result.put("encrtptType","1");
//        Map<String,Object> appInfo = new HashMap<>();
//        appInfo.put("frontEndUrl","xxx");
//        appInfo.put("adminUrl","xxx");
//        appInfo.put("userName",HuaweiEncryptUtil.encryptAESCBCEncode("xxxxxxxxsxxxxxxxxxxxx","908fc823-65e0-47e2-b520-18860e618e37","",128));
//        appInfo.put("password",HuaweiEncryptUtil.encryptAESCBCEncode("xxxxxxxxsxxxxxxxxxxxx","908fc823-65e0-47e2-b520-18860e618e37","",128));
//        result.put("appInfo",appInfo);
//        response.addHeader("sign_type", "\"HMAC-SHA256\"");
//        response.addHeader("Body-Sign","sign_type=\"HMAC-SHA256\""+",signature=\""+ HuaweiEncryptUtil.base_64(HuaweiEncryptUtil.hmacSHA256("908fc823-65e0-47e2-b520-18860e618e37", JsonUtil.toJSON(result)))+"\"");
//        return result;
//    }
//
//    /**
//     * <desc>
//     *
//     * </desc>
//     *
//     * @param request
//     * @return
//     * @author Jiaqi.X
//     * @createDate 2020/04/07
//     */
//    @RequestMapping("/refreshInstance")
//    @ResponseBody
//    @UnAuthAccessRequired
//    public Object valid2(HttpServletRequest request,HttpServletResponse response) throws IOException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException {
//        Map<String,Object> result = new HashMap<>();
//        result.put("resultCode", "000000");
//        result.put("resultMsg", "success");
//        response.addHeader("sign_type", "\"HMAC-SHA256\"");
//        response.addHeader("Body-Sign","sign_type=\"HMAC-SHA256\""+",signature=\""+ HuaweiEncryptUtil.base_64(HuaweiEncryptUtil.hmacSHA256("908fc823-65e0-47e2-b520-18860e618e37", JsonUtil.toJSON(result)))+"\"");
//        return result;
//    }
//
//    /**
//     * <desc>
//     *
//     * </desc>
//     *
//     * @param request
//     * @return
//     * @author Jiaqi.X
//     * @createDate 2020/04/07
//     */
//    @RequestMapping("/expireInstance")
//    @ResponseBody
//    @UnAuthAccessRequired
//    public Object valid3(HttpServletRequest request,HttpServletResponse response) throws IOException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException {
//        Map<String,Object> result = new HashMap<>();
//        result.put("resultCode", "000000");
//        result.put("resultMsg", "success");
//        response.addHeader("sign_type", "\"HMAC-SHA256\"");
//        response.addHeader("Body-Sign","sign_type=\"HMAC-SHA256\""+",signature=\""+ HuaweiEncryptUtil.base_64(HuaweiEncryptUtil.hmacSHA256("908fc823-65e0-47e2-b520-18860e618e37", JsonUtil.toJSON(result)))+"\"");
//        return result;
//    }
//
//    /**
//     * <desc>
//     *
//     * </desc>
//     *
//     * @param request
//     * @return
//     * @author Jiaqi.X
//     * @createDate 2020/04/07
//     */
//    @RequestMapping("/releaseInstance")
//    @ResponseBody
//    @UnAuthAccessRequired
//    public Object valid4(HttpServletRequest request,HttpServletResponse response) throws IOException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException {
//        Map<String,Object> result = new HashMap<>();
//        result.put("resultCode", "000000");
//        result.put("resultMsg", "success");
//        response.addHeader("sign_type", "\"HMAC-SHA256\"");
//        response.addHeader("Body-Sign","sign_type=\"HMAC-SHA256\""+",signature=\""+ HuaweiEncryptUtil.base_64(HuaweiEncryptUtil.hmacSHA256("908fc823-65e0-47e2-b520-18860e618e37", JsonUtil.toJSON(result)))+"\"");
//        return result;
//    }
}